package com.honglu.backend.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.honglu.backend.common.Constants;
import com.honglu.backend.common.controller.BaseController;
import com.honglu.backend.entity.StartGraph;
import com.honglu.backend.service.impl.StartGraphServiceImpl;
import com.honglu.backend.utils.CodeUtils;
import com.honglu.backend.utils.DateUtils;
import com.honglu.backend.utils.JSONUtils;
import com.honglu.utils.PropertiesUtil;
import com.honglu.utils.SpringUtils;

@Controller
@RequestMapping("/backend/web/startGraph/")
public class StartGraphController extends BaseController {
	private static Logger logger = Logger.getLogger(StartGraphController.class);
	@Autowired
	private StartGraphServiceImpl startGraphService;

	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getAllStartGraph.do", method = RequestMethod.POST)
	public void getAllBootDiagram(HttpServletRequest request, HttpServletResponse response, StartGraph startGraph) {
		try {
			int count = startGraphService.listAllCount(startGraph);// 统计总条数
			List<StartGraph> list = startGraphService.getAll(startGraph);
			JSONUtils.toListJSON(response,list, count);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
		}
	}

	/**
	 * 根据id获取对象信息
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param banner
	 */

	@RequestMapping(value = "getEntityById.do", method = RequestMethod.POST)
	public void getEntityById(HttpServletRequest request, HttpServletResponse response, StartGraph startGraph) {
		try {
			StartGraph startGraphBo = startGraphService.selectByPrimaryKey(startGraph.getGraphId());
			LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
			LinkedHashMap<String, Object> map1 = new LinkedHashMap<String, Object>();
			map.put(Constants.CODE, CodeUtils.SUCCESS.getCode());
			map.put(Constants.MESSAGE, CodeUtils.SUCCESSNEWS.getMessage());
			map1.put(Constants.SUCCESSED, CodeUtils.SUCCESS.getFlag());
			map1.put("bootDiagramObject", startGraphBo); // 传入对象
			map.put(Constants.DATA, map1);
			SpringUtils.renderJson(response, map);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
		}
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @param banner
	 */
	@RequestMapping(value = "delete.do")
	public void delete(HttpServletRequest request, HttpServletResponse response, StartGraph startGraph) {
		try {
			boolean flag = startGraphService.deleteByPrimaryKey(startGraph.getGraphId());
			if (flag) {// 成功
				JSONUtils.toJSON(response, CodeUtils.DELETE_SUCCESS);
				return;
			} else {// 失败
				JSONUtils.toJSON(response, CodeUtils.DELETE_FAIL);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
		}

	}

	/**
	 * 新增/修改
	 * 
	 * @param request
	 * @param response
	 * @param banner
	 */

	@RequestMapping(value = "saveStartGraph.do")
	public void saveBootDiagram(HttpServletRequest request, HttpServletResponse response, StartGraph startGraph) {
		try {
			if (startGraph != null && startGraph.getGraphId() != null
					&& !"".equals(startGraph.getGraphId())) {// 修改
				boolean flag = startGraphService.updateByPrimaryKeySelective(startGraph);
				if (flag) {
					JSONUtils.toJSON(response, CodeUtils.UPDATE_SUCCESS);
					return;
				} else {
					JSONUtils.toJSON(response, CodeUtils.UPDATE_FAIL);
					return;
				}
			} else {// 新增
				boolean flag = startGraphService.insertSelective(startGraph);
				if (flag) {// 成功
					JSONUtils.toJSON(response, CodeUtils.SAVE_SUCCESS);
					return;
				} else {// 失败
					JSONUtils.toJSON(response, CodeUtils.SAVE_FAIL);
					return;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			JSONUtils.toJSON(response, CodeUtils.BACKEND_ERROR);
		}
	}

	/**
	 * 文件上传
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "upload.do", method = RequestMethod.POST)
	public void upload(HttpServletRequest request, HttpServletResponse response) {
		try  {
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multiRequest.getFile("file");

			String fileVal = uploadFilesOss(file, PropertiesUtil.get("file.startGraph"));
			if (StringUtils.isNotEmpty(fileVal) && "error".equals(fileVal)) {
				JSONUtils.toJSON(response, CodeUtils.IMAGE_UPLOAD_FAILUE_TYPE);
				return;
			}
			JSONUtils.toJSON(response, CodeUtils.SUCCESS, fileVal);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			// TODO: handle exception
		}

	}
	

	/**
	 *startGraph接口
	 * 
	 * @param request
	 * @param response
	 */

	@RequestMapping(value = "interfaceStartGraph.do", method = RequestMethod.POST)
	public void interfaceBanner(HttpServletRequest request, HttpServletResponse response) {
		List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
		List<StartGraph> list = startGraphService.findStartGraph(DateUtils.parseToDateTimeStr(new Date()));
		if (list == null || ("").equals(list) || list.size() < 0) {
			JSONUtils.toJSON(response, CodeUtils.QUERY_NULL);
			return;
		} else if(list !=null || !("").equals(list) || list.size() > 0){
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("title", list.get(i).getTitle());
				map.put("image", list.get(i).getImage());
				map.put("url", list.get(i).getUrl());
				map.put("isStatic", list.get(i).getIsStatic());
				map.put("mobileType", list.get(i).getMobileType());
				map.put("type", list.get(i).getType());
				list1.add(map);
			}
			JSONUtils.toJSON(response, CodeUtils.QUERY_SUCCESS, list1);
			return;
		}
	  	 
	}

}
